<?php

namespace Biz\model\brand;

use Doraemon\pockets\datebase\BaseDbModel;

/**
 * 品牌内容检索词
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     zhouyang  <congyukun@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */
class BrandContentKeywords extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';
    
    const _CONFIG_SELECT_ = 'db_store';
    
    const _DATABASE_ = 'store';
    
    const _TABLE_NAME = 'brand_content_keywords';
    
    const _SUB_TABLE = false;
    
    const  STATUS_NORMAL_ON = 1; //正常
    const  STATUS_NORMAL_OFF = -1; //关闭
    const  SEARCH_TYPE_KEYWORDS = 3; //检索词类型
    
    
    /**
     * 新增
     * @param $data
     * @return bool|\PDOStatement
     */
    public static function create($data)
    {
        $rs = self::insert($data)->rowCount();
        if ($rs) {
            return self::id();
        }
    }
    
    /**
     * 更新或新增
     * @param $where
     * @param $data
     * @return bool|\PDOStatement
     */
    public static function updateOrCreate($where, $data)
    {
        $bool = self::get("id", $where);
        $data['auto_up_time'] = date("Y-m-d 00:00:00", strtotime("+1 day"));
        $data['update_time'] = date("Y-m-d H:i:s");
        if (!$bool) {
            $data['create_time'] = date("Y-m-d H:i:s");
            $rs = self::insert($data)->rowCount();
            if ($rs) {
                return self::id();
            }
        }
        
        return self::update($data, $where)->rowCount();
        
    }
    
    /**
     * 获取单个字段
     * @param $where
     * @param string $filed
     * @return array|mixed
     */
    public static function first($where, $filed = 'id')
    {
        return self::get($filed, $where);
    }
    
    /**
     * 获取检索词数组
     * @param $brand_id
     * @return array|false|string[]
     */
    public static function getWordsById($brand_id)
    {
        $where['brand_id'] = $brand_id;
        $where['status'] = self::STATUS_NORMAL_ON;
        $words = self::get('search_keywords', $where);
        $res = [];
        if (!empty($words)) {
            $res = explode(',', $words);
        }
        return $res;
    }
    
    
}
